﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoAddonBiz_FreightForwarder.Air.AirPortWork.Warehouse
{
    [BoAddonBiz_FreightForwarder.BusinessBase.ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_FF_BJSWarehouse_OUT)]
    class B1Form_Warehouse_Out : SAPBoAddon.B1Form
    {
        #region override
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_BJSWarehouse_OUT;
            FormSetting.FormFileName = "AVA_FF_StorageBJSOutForm.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {

        }
        #endregion
        #region constructor
        public B1Form_Warehouse_Out()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Warehouse_Out_frm_ItemEvent);
        }

        void B1Form_Warehouse_Out_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction == false)
            {
                #region 查询
                if (pVal.ItemUID == "12" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    string SerialNum = CurrentForm.DataSources.UserDataSources.Item("SerialNum").Value;
                    string DocEntry = CurrentForm.DataSources.UserDataSources.Item("DocEntry").Value;
                    string CardName = CurrentForm.DataSources.UserDataSources.Item("CardName").Value;
                    string MBLNumber = CurrentForm.DataSources.UserDataSources.Item("MBLNumber").Value;
                    string SBLNumber = CurrentForm.DataSources.UserDataSources.Item("SBLNumber").Value;
                    SAPbouiCOM.DataTable dt;
                    try
                    {
                        dt = CurrentForm.DataSources.DataTables.Add("result");
                    }
                    catch
                    {
                        dt = CurrentForm.DataSources.DataTables.Item("result");
                    }
                    string SQL = @"
                                    SELECT T0.DocEntry,T1.LineId,'N' Choose,U_BaseSerial 业务单号,U_BaseEntry 流水号,U_CardName 客户,U_CntctCode 联系人,U_MBLNumber 主单,U_SBLNumber 分单,U_DocDate 入库日期,U_CargoType 货物类型
	                                    ,U_CargoName 货物,U_ShipMark 唛头,U_Warehouse 库位,U_Quantity 数量,U_Weight 重量,U_Volume 体积,U_PkCdtion 包装情况,U_BrRecord 破损记录,U_PackRqmt 包装要求,U_MarkReco 标记
	                                    ,U_WhsFee 费率,U_StoState 状态,U_WhsDays 天数,T0.U_Remarks 备注
                                    FROM [@AVA_FF_OSTG] T0
	                                    INNER JOIN [@AVA_FF_STG1] T1 ON T0.DocEntry = T1.DocEntry
                                    Where T0.Canceled = 'N' And T0.U_Status <> 'D'
	                                    And T1.U_OutOrIn = 'In'
	                                    And (U_BaseSerial Like '%{0}%' Or '{0}' = '')
	                                    And (U_BaseEntry Like '%{1}%' Or '{1}' = '')
	                                    And (U_CardName Like '%{2}%' Or '{2}' = '')
	                                    And (U_MBLNumber Like '%{3}%' Or '{3}' = '')
	                                    And (U_SBLNumber Like '%{4}%' Or '{4}' = '')                    
                                                        ";
                    SQL = string.Format(SQL, SerialNum, DocEntry, CardName, MBLNumber, SBLNumber);
                    dt.ExecuteQuery(SQL);

                    SAPbouiCOM.Grid oGrid = CurrentForm.Items.Item("Grid").Specific;
                    oGrid.DataTable = dt;
                    oGrid.Columns.Item(0).Visible = false;
                    oGrid.Columns.Item(1).Visible = false;
                    oGrid.Columns.Item(2).Type = SAPbouiCOM.BoGridColumnType.gct_CheckBox;
                    if (oGrid.Columns.Count > 3)
                        for (int i = 3; i < oGrid.Columns.Count; i++)
                        {
                            oGrid.Columns.Item(i).Editable = false;
                        }
                }
                #endregion
                #region 出库
                if (pVal.ItemUID == "13" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    SAPbouiCOM.Grid oGrid = CurrentForm.Items.Item("Grid").Specific;
                    IList<Cargo> cargoList = new List<Cargo>();
                    Cargo cargo = null;
                    for (int i = 0; i < oGrid.Rows.Count; i++)
                    {
                        if (oGrid.DataTable.GetValue("Choose", i).ToString().Trim() == "Y")
                        {
                            cargo = new Cargo();
                            cargo.DocEntry = oGrid.DataTable.GetValue("DocEntry", i).ToString().Trim();
                            cargo.LineId = oGrid.DataTable.GetValue("LineId", i).ToString().Trim();
                            cargoList.Add(cargo);
                        }
                    }
                    SAPBoAddon.B1FormInitializePar init = new SAPBoAddon.B1FormInitializePar();
                    init.parObject = cargoList;
                    ShowForm(B1FormTypes.ado_AVA_FF_BJSDelivery, init);
                }
                #endregion
            }
        }
        #endregion
    }
    internal class Cargo
    {
        public string DocEntry { get; set; }
        public string LineId { get; set; }
    }
}
